/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user.internacion;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.jboss.logging.Logger;

import ar.com.greensoft.jeecommons.web.jsf.util.JSFMenuUtil;
import ar.com.greensoft.neosystem.crm.web.beans.NavigationConstants;
import ar.com.greensoft.neosystem.dao.DaoException;
import ar.com.greensoft.neosystem.dao.InternacionDao;
import ar.com.greensoft.neosystem.dao.PersonaFisicaDao;
import ar.com.greensoft.neosystem.model.PersonaFisica;
import ar.com.greensoft.neosystem.model.internacion.Internacion;
import ar.com.greensoft.neosystem.model.references.EstadoInternacion;
import ar.com.greensoft.neosystem.model.references.Sexo;

/**
 * 
 * @author Jorge E. Villaverde
 * @date 23/06/2013
 * @version 1.0.0
 */
@ManagedBean
@ViewScoped
public class InternacionCreateBean implements Serializable, NavigationConstants{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger logger = Logger.getLogger(InternacionCreateBean.class);
	
	// Properties ---------------------------------------------------------------------------------
	
	private Internacion internacion;
	
	// Dependencies -------------------------------------------------------------------------------
	@EJB
	private InternacionDao dao;
	@EJB
	private PersonaFisicaDao personaFisicaDao;
	
	// Actions ------------------------------------------------------------------------------------
	
	@PostConstruct
	private void init(){
		internacion = new Internacion();
		internacion.setFecha(new Date());
		internacion.setEstado(EstadoInternacion.ACTIVA);
	}

	public String create(){
		try {
			PersonaFisica madre = personaFisicaDao.findById(internacion.getMadre().getId());
			internacion.setMadre(madre);
			dao.merge(internacion);
			return INDEX_ACTION;
		} catch (DaoException e) {
			logger.error(e.getMessage());
			JSFMenuUtil.addErrorMessage(e.getMessage());
		}
		return null;
	}

	// Getters/setters ----------------------------------------------------------------------------

	public Internacion getInternacion() {
		return internacion;
	}

	public void setInternacion(Internacion internacion) {
		this.internacion = internacion;
	}
	
	/**
	 * @return Personas Físicas que pueden ser madres
	 */
	public List<PersonaFisica> getMadres(){
		return personaFisicaDao.findBySexo(Sexo.MUJER);
	}
}
